<div id="header"><p>Your Room</p></div>

<?php
	
	// Including this script creates the sensor select panel at the top of the page.
	// Allowing the user to select a sensor.  This script defines the 'sensorID' session var.
	include("sensor_select.php");

	// Check for errors and inform the user of the issue
	if($database->errno != 0)
	{
		echo '<div id="subheader">Database Error</div>';
		echo "<p> We are unable to contact the database.  We apologize for the inconvenience. Please try again later.</p>";
		return;
	}

	// If there is no sensorID set (by the sensor_select.php script), then there aren't any sensors associated with the system
	if(!isset($_SESSION['sensorID']))
	{
		echo '<div id="subheader">Sensor Error</div>';
		echo '<p> There are currently no sensors associated with this system.</p>';
		return;
	}
	
	// Get the color assocations for temperature levels
	$colors = get_temp_colors();

	// Define a color for warnings
	$badColor = "#CC2222";

	// Get the latest data of the current selected sensor
	$resultSet = $database->multi_query("CALL getLatestDataOfSensor(".$_SESSION['sensorID'].",1)");
	$output = "";

	// If no result set was returned, then there was a database error
	if(!$resultSet)
	{
		$headingColor = $badColor;
		$output .= '<p>Database error, please try again later.</p>';
	}
	else
	{
		// Get the result set
		$resultSet = $database->use_result();
		
		// Go through each row in result set (should only be one)
		while($row = $resultSet->fetch_row())
		{
			// Determine temperature color set the heading
			$tempColor = assign_temp_color($colors,(float)$row[4]);
			$headingColor = $tempColor;

			// Add temperature HTML
			$output .= '<div id="currentdata">';
			$output .= '<div id="temperature" style="color:'.$tempColor.'">
									<div id="popup">It\'s currently '.$row[4].'&#176</div>
									<p>'.$row[4].'&#176</p></div>';

			// Depending on light state, set the correct lightbulb icon
			if($row[5] == "on")
			{
				$output .= '<div id="light">
								<p id="popup">The light is on</p>
								<img src="icons/lightbulb.png" title="The Light is On"/>
							</div>';
			}
			else
			{
				$output .= '<div id="light">
								<div id="popup">The light is off</div>
								<img src="icons/lightbulb_off.png" title="The Light is Off"/>
							</div>';
			}
			
			// Output the door state
			$output .=  '<div id="door"><span id="popup">The door is '.$row[6].'</span>Door: '.$row[6].'</div>';
			$output .=  '<br/>';
			$output .=  "</div>";
		}

		// "Read" the rest of the results (Stored procedures return a second result set)
		if($database->more_results())
  		while($database->next_result());

		// If there was no output, then there must not be data available for this sensor
		if($output === "")
		{
			$output = "<p>There is currently no data available for this sensor.</p>";		
		}
	}

	// Echo out the subheader and the just create HTML for the current state
	echo "<div id=\"subheader\" style=\"background-color:$headingColor\">Currently</div>";
	echo $output;

	// Now, output the recent history
	echo ' <div id="subheader">Recent History</div>';

	// Create a table of past 10 data points
	$results = $database->multi_query("CALL getLatestDataOfSensor(".$_SESSION['sensorID'].",10)");
	
	// Create an array of the data to pass to the graphing function
	$graphResults = array();

	// If there is data...
	if($results)
	{
		$results = $database->use_result();

		// Output our table...
		echo '<table>';
		echo '<tr><th>Date</th><th>Time</th><th>Temperature</th><th>Light</th><th>Door</th></tr>';
		while($row = $results->fetch_row())
		{
			// Store this data in the graphing array for later use
			$graphResults[] = $row;
			echo '<tr>';
			echo '<td>'.date("Y-m-d",strtotime($row[2])).'</td>';
			echo '<td>'.date("g:i:s A",strtotime($row[3])).'</td>';
			echo '<td>'.$row[4].'</td>';
			echo '<td>'.$row[5].'</td>';
			echo '<td>'.$row[6].'</td>';	
			echo '</tr>';
			
		}
		echo '</table>';
    
		// Include our graphing functionality
		include("graphs.php");
		
		// Generate the graph using the data in the table
		graph_results(500,200,$graphResults, $_SESSION['sensorID']);

		// Link the image of the just-generated graph
    echo '<img src="icons/'.$_SESSION['sensorID'].'graph.png"></img>';
	}
	else
	{
		echo '<p>There was a database error:'.$database->error.'</p>';
	}
?>
